// src/router/index.ts
import { createRouter, createWebHistory } from 'vue-router'
// 方式1：使用 @ 别名（记得之前已在 tsconfig 和 vite.config 里配置过）
import { getCurrentUser } from '../utils/storage'

const routes = [
  { path: '/login', name: 'Login', component: () => import('@/views/LoginView.vue') },
  { path: '/register', name: 'Register', component: () => import('@/views/RegisterView.vue') },
  {
    path: '/',
    name: 'Chat',
    component: () => import('@/views/ChatView.vue'),
    meta: { requiresAuth: true }
  },
  { path: '/:pathMatch(.*)*', redirect: '/login' }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to) => {
  if (to.meta.requiresAuth && !getCurrentUser()) {
    return { name: 'Login' }
  }
})

export default router
